package com.ruoyi.web.controller.account;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SecurityValveAccount;
import com.ruoyi.system.domain.vo.DeleteVo;
import com.ruoyi.system.service.SecurityValveAccountService;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 安全阀台账(SecurityValveAccount)表控制层
 *
 * @author makejava
 * @since 2022-12-14 12:00:12
 */
@RestController
@RequestMapping("security/valve/account")
public class SecurityValveAccountController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SecurityValveAccountService securityValveAccountService;

    /**
     * 分页查询
     *
     * @param securityValveAccount 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public TableDataInfo queryByPage(SecurityValveAccount securityValveAccount) {
        startPage();
        List<SecurityValveAccount> list = securityValveAccountService.queryAll(securityValveAccount);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SecurityValveAccount> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.securityValveAccountService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param securityValveAccount 实体
     * @return 新增结果
     */
    @PostMapping("add")
    @PreAuthorize("@ss.hasPermi('security:valve:account:add')")
    public ResponseEntity<SecurityValveAccount> add(@Validated @RequestBody SecurityValveAccount securityValveAccount) {
        return ResponseEntity.ok(this.securityValveAccountService.insert(securityValveAccount));
    }

    /**
     * 编辑数据
     *
     * @param securityValveAccount 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    @PreAuthorize("@ss.hasPermi('security:valve:account:edit')")
    public ResponseEntity<SecurityValveAccount> edit(@Validated @RequestBody SecurityValveAccount securityValveAccount) {
        return ResponseEntity.ok(this.securityValveAccountService.update(securityValveAccount));
    }

    /**
     * 删除数据
     *
     * @param deleteVo 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    @PreAuthorize("@ss.hasPermi('security:valve:account:delete')")
    @ApiOperation("删除")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody DeleteVo deleteVo) {
        return ResponseEntity.ok(this.securityValveAccountService.deleteById(deleteVo.getId()));
    }

    @Log(title = "安全阀台账(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @PreAuthorize("@ss.hasPermi('security:valve:account:importData')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SecurityValveAccount> util = new ExcelUtil<>(SecurityValveAccount.class);
        List<SecurityValveAccount> securityValveAccounts = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = securityValveAccountService.importExcel(securityValveAccounts, updateSupport, operName);
        return success(message);
    }

    @Log(title = "安全阀台账(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('security:valve:account:export')")
    public void export(HttpServletResponse response, SecurityValveAccount securityValveAccount) {
        List<SecurityValveAccount> list = securityValveAccountService.queryAll(securityValveAccount);
        ExcelUtil<SecurityValveAccount> util = new ExcelUtil<>(SecurityValveAccount.class);
        util.exportExcel(response, list, "安全阀台账");
    }

    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('security:valve:account:importTemplate')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SecurityValveAccount> util = new ExcelUtil<>(SecurityValveAccount.class);
        util.importTemplateExcel(response, "安全阀台账");
    }

}

